package view.components
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import global.Resource;
	
	import utils.TextTool;
	
	public class Radio extends Button
	{
		public static var tempText:TextField;
		
		protected var offState:DisplayObject;
		protected var onState:DisplayObject;
		private var _selected:Boolean;
		private var title:TextField;
		
		public function Radio(x:int,y:int,id:int,onClickFunction:Function)
		{
			super(x,y,id,onClickFunction);
			this.mouseChildren = false;
			initTempText();
			initState();	
			initTitle();
		}
		
		private function initTempText():void
		{
			if (tempText == null)
			{
				tempText = TextTool.getTextField(600,20,12,0xFFFFFF);
			} 
		}
		
		private function initTitle():void
		{
			title = TextTool.getTextField(100,20,12,0xFFFFFF);
			title.x = onState.x + onState.width + 2;
			addChild(title);
		}
		
		public function setTitle(text:String):void
		{
			tempText.text = text;
			title.text = text;
			title.width = tempText.textWidth + 10;
		}

		public function get selected():Boolean
		{
			return _selected;
		}

		public function set selected(v:Boolean):void
		{
			_selected = v;
			offState.visible = !v;
			onState.visible = v;
		}

		protected function initState():void
		{
			offState = Resource.getImg("face_radioOff");
			onState = Resource.getImg("face_radioOn");
			
			addChild(onState);
			addChild(offState);
			onState.visible = false;
			
			onState.y = offState.y = 3;
			
			if (onState is TempBitmap)
			{
				onState.addEventListener(Event.COMPLETE,onOnStateComplete);
			}
		}
		
		protected function onOnStateComplete(event:Event):void
		{
			onState.removeEventListener(Event.COMPLETE,onOnStateComplete);
			title.x = onState.x + onState.width + 2;
		}
		
		override protected function onMouseClick(event:MouseEvent) : void
		{
			super.onMouseClick(event);
			offState.visible = !offState.visible;
			onState.visible = !onState.visible;
			_selected = onState.visible;
		}		
	}
}